﻿angular.module('picShare').controller('myImagesController', function ($scope, httpRequestSvc, userDataSvc, safeApply, $rootScope,Repository) {
    $scope.title = "My Images Page";

    var counter = 4;
    var items = 4;
    var albums = [];
//
//    $scope.$watch(function () {
//        return Repository.table;
//    }, function (newVal) {
//        console.log(Repository.table);
//        $scope.albums = newVal;
//    });

   // Repository.table = {};


    userDataSvc.user.userAlbumsAndPictures = [];

    $scope.showImages = function ($event) {
        var targ = $event.target.parentElement.parentElement;
        console.log(targ);
        $(targ).css("height", "auto");
    };

    $scope.hideImages = function ($event) {
        var targ = $event.target.parentElement.parentElement.parentElement;
        console.log(targ);
        $(targ).css("height", "100px");
    };

    if (userDataSvc.user.albumNames != null) {
        var alb = userDataSvc.user.albumNames;
        for (var i = 0; i < alb.length; i++) {
            albums.push(alb[i].name);
        }
    }

    var len;
    function getImg(albumName, count) {

        httpRequestSvc.getJson("http://localhost:26666/api/Image/load-images-from-album?userName="
        + userDataSvc.user.userName + "&albumName=" + albumName + "&counter=" + count + "&itemsCount=" + items).done(
            function (data) {

                len = data.albumLength;

                if (count > len) {
                    counter = 4;
                    return;
                }

                for (var key in userDataSvc.user.userAlbumsAndPictures) {
                    if (key == albumName) {
                        userDataSvc.user.userAlbumsAndPictures[key] = data.images;
                    }
                }

                safeApply($rootScope);
            }, function (data) {
                alert(JSON.stringify(data));
            });
    }

    $scope.goForward = function (albumName) {

        if (counter >= len) {
            counter = 0;
        }

        getImg(albumName, counter);


        counter = counter + items;

    };

    $scope.deleteImage = function (albumName, index) {

        httpRequestSvc.postJson("http://localhost:26666/api/Image/delete-image?userName=" + userDataSvc.user.userName + "&albumName=" + albumName + "&index=" + index)
               .done(function (data) {
                   // console.log(data);
                   $scope.$apply();
               }, function (err) {
                   alert(JSON.stringify(err));
               });

    };

    function update(data) {
        var delay = 1000;

        var a = data.length;

        for (var key in data) {
            var obj = data[key];
            var objName = key;

            for (var j = 0; j < obj.length; j++) {

                setTimeout((function (s,name) {

                    return function () {
                        var albumName = name;
                        Repository.setItem(albumName, s);
                        $scope.albums = Repository.table;
                        safeApply($scope);
                    };

                })(obj[j],objName), delay);

                delay += 1000;
            }

        }
    }


    httpRequestSvc.postJson("http://localhost:26666/api/Image/load-user-images?userName=" + userDataSvc.user.userName + "&counter=0&itemsCount=4", albums)
    .done(function (data) {
        console.log(data);
        //  TODO      implement for loop to slow binding
       
        update(data);


        //  userDataSvc.user.userAlbumsAndPictures = data;


    }, function (err) {
        alert(JSON.stringify(err));
    });

});



















